/******************************************************************************/
/* [[--]]                                                                     */
/*          ::::::::::::::::                                                  */
/*       :::::::::::::########:                                               */
/*     :::::::::::::############:                                             */
/*    :::::::::::..##############:                                            */
/*   ::::::::::...################:                                           */
/*  :::::::::.....################.:                                          */
/*  :::::::::......##############..:  This product is protected under         */
/*  ::::::::........############...:  copyright law                           */
/*  ::::::::..........########.....:                                          */
/*   ::::::::.....................:   (c) Copyright 2008-2009, FlexyCore SAS. */
/*    ::::::::...................:                                            */
/*     ::::::::.................:     All rights reserved.                    */
/*       :::::::::............:                                               */
/*          ::::::::::::::::                                                  */
/* [[++]]                                                                     */
/******************************************************************************/

package com.flexycore.iphoneapps.weather;

import com.flexycore.iphone.corelocation.CLLocation;
import com.flexycore.iphone.corelocation.CLLocationManager;
import com.flexycore.iphone.corelocation.CLLocationManagerDelegate;
import com.flexycore.iphone.uikit.UIApplication;
import com.flexycore.iphone.uikit.UIApplicationDelegate;
import com.flexycore.iphone.uikit.UIScreen;
import com.flexycore.iphone.uikit.UIWindow;

public class WeatherApps extends UIApplicationDelegate {
	private UIWindow window;

	public void applicationWillTerminate(UIApplication application) {
	}

	public void applicationDidFinishLaunching(UIApplication application) {
		// Construct a waiting view
		final WaitView waitView;
		waitView = new WaitView();

		// Construct Window, add the Waiting view and make it visible
		window = new UIWindow();
		window.initWithFrame(UIScreen.mainScreen().bounds());
		window.addSubview(waitView.view());
		window.makeKeyAndVisible();

		// Start a Location manager to get GPS location
		CLLocationManager manager = new CLLocationManager();
		manager.init();
		manager
				.setDesiredAccuracy(CLLocation.kCLLocationAccuracyThreeKilometers);
		manager.setDelegate(new CLLocationManagerDelegate() {
			public void locationManagerDidUpdateToLocationFromLocation(
					CLLocationManager manager, CLLocation newLocation,
					CLLocation oldLocation) {

				// When we got a location, stop location manager ...
				manager.stopUpdatingLocation();

				// And replace the waiting view with a weather view
				waitView.view().removeFromSuperview();
				window
						.addSubview(new WeatherView(newLocation.coordinate()
								.getLatitude(), newLocation.coordinate()
								.getLongitude()).view());
			}
		}.init());
		manager.startUpdatingLocation();
	}
}
